package com.hhz.time.part2;

import org.junit.Test;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

/**
 * 时间校正器
 *
 * @Author Rem
 * @Date 2020-02-15
 */

public class TestTemporaAdjuster {

    @Test
    public void test() {

        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时间：" + now);

        System.out.println("指定日改为8号：" + now.withDayOfMonth(8));

        TemporalAdjuster temporalAdjuster = new TemporalAdjuster() {
            @Override
            public Temporal adjustInto(Temporal temporal) {
                return null;
            }
        };

        System.out.println("当年的最后一天：" + now.with(TemporalAdjusters.lastDayOfYear()));
        System.out.println("本月的第一天：" + now.with(TemporalAdjusters.firstDayOfMonth()));
        System.out.println("下个星期天：" + now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)));


        //下个工作日
        LocalDateTime workday = now.with(temporal -> {
            LocalDateTime ldt = (LocalDateTime) temporal;

            DayOfWeek dayOfWeek = ldt.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
                return ldt.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
                return ldt.plusDays(2);
            } else if (dayOfWeek.equals(DayOfWeek.SUNDAY)) {
                return ldt.plusDays(1);
            } else {
                return ldt;
            }
        });

        System.out.println("下个工作日：" + workday);
    }
}
